package cn.iocoder.yudao.module.wengfu.controller.admin.approval;

import cn.iocoder.yudao.module.wengfu.framework.excel.core.util.ExcelUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wengfu.controller.admin.approval.vo.*;
import cn.iocoder.yudao.module.wengfu.dal.dataobject.approval.SampleApprovalInfoDO;
import cn.iocoder.yudao.module.wengfu.service.approval.SampleApprovalInfoService;

@Tag(name = "管理后台 - 样品审批")
@RestController
@RequestMapping("/wengfu/sample-approval-info")
@Validated
public class SampleApprovalInfoController {

    @Resource
    private SampleApprovalInfoService sampleApprovalInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建样品审批")
    @PreAuthorize("@ss.hasPermission('wengfu:sample-approval-info:create')")
    public CommonResult<Long> createSampleApprovalInfo(@Valid @RequestBody SampleApprovalInfoSaveReqVO createReqVO) {
        return success(sampleApprovalInfoService.createSampleApprovalInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新样品审批")
    @PreAuthorize("@ss.hasPermission('wengfu:sample-approval-info:update')")
    public CommonResult<Boolean> updateSampleApprovalInfo(@Valid @RequestBody SampleApprovalInfoSaveReqVO updateReqVO) {
        sampleApprovalInfoService.updateSampleApprovalInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除样品审批")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wengfu:sample-approval-info:delete')")
    public CommonResult<Boolean> deleteSampleApprovalInfo(@RequestParam("id") Long id) {
        sampleApprovalInfoService.deleteSampleApprovalInfo(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除样品审批")
                @PreAuthorize("@ss.hasPermission('wengfu:sample-approval-info:delete')")
    public CommonResult<Boolean> deleteSampleApprovalInfoList(@RequestParam("ids") List<Long> ids) {
        sampleApprovalInfoService.deleteSampleApprovalInfoListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得样品审批")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wengfu:sample-approval-info:query')")
    public CommonResult<SampleApprovalInfoRespVO> getSampleApprovalInfo(@RequestParam("id") Long id) {
        SampleApprovalInfoDO sampleApprovalInfo = sampleApprovalInfoService.getSampleApprovalInfo(id);
        return success(BeanUtils.toBean(sampleApprovalInfo, SampleApprovalInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得样品审批分页")
    @PreAuthorize("@ss.hasPermission('wengfu:sample-approval-info:query')")
    public CommonResult<PageResult<SampleApprovalInfoRespVO>> getSampleApprovalInfoPage(@Valid SampleApprovalInfoPageReqVO pageReqVO) {
        PageResult<SampleApprovalInfoDO> pageResult = sampleApprovalInfoService.getSampleApprovalInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SampleApprovalInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出样品审批 Excel")
    @PreAuthorize("@ss.hasPermission('wengfu:sample-approval-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSampleApprovalInfoExcel(@Valid SampleApprovalInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SampleApprovalInfoDO> list = sampleApprovalInfoService.getSampleApprovalInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "样品审批.xls", "数据", SampleApprovalInfoRespVO.class,
                        BeanUtils.toBean(list, SampleApprovalInfoRespVO.class));
    }

}
